import com.mysql.cj.jdbc.MysqlDataSource;
import jdk.nashorn.internal.runtime.regexp.joni.exception.SyntaxException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2023-11-15  20:08
 */
public class JDBCDelete {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的ID:");
        int id = scanner.nextInt();
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("@Tao126826.");

        Connection connection = dataSource.getConnection();

        String sql = "delete from student where id = (?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);

        int n = statement.executeUpdate();
        System.out.println("n = " + n);

        statement.close();
        connection.close();
    }
}
